﻿using System;
using System.Linq;
using Ones.ServiceAdvertisementManagement;

namespace Ones.WebAdmin
{
    public partial class AdsMgmtAdsPage : System.Web.UI.Page
    {
        private ServiceAdvertisementManagementClient c = null;
        private Advertisement ads = null;
        private int itemId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadContact();
                loadBlock();

                if (Session["cmd"] != null)
                {
                    fillData();

                    if (Session["cmd"].ToString().Equals("delete"))
                    {
                        cbbContact.Enabled = false;
                        cbbBlock.Enabled = false;
                        txtDateExpire.Enabled = false;
                        txtLink.Enabled = false;
                        txtImageLink.Visible = true;
                        txtImageLink.Enabled = false;
                        FileUpload1.Enabled = false;
                    }
                }
            }
        }
  
        public void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                ads = new Advertisement();

                ads.advContId = int.Parse(cbbContact.SelectedValue.ToString());
                ads.advBlockId = cbbBlock.SelectedValue;
                ads.advDateExpire = DateTime.Parse(txtDateExpire.Text);
                ads.advLink = txtLink.Text;
                ads.advImageLink = "ads-default.png";
                if (FileUpload1.HasFile)
                {
                    ads.advImageLink = FileUpload1.FileName;
                }

                if (c.addAdvertisment(ads))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListAdsPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                ads = new Advertisement();

                ads.advId = int.Parse(Session["id"].ToString());
                ads.advContId = int.Parse(cbbContact.SelectedValue.ToString());
                ads.advBlockId = cbbBlock.SelectedValue;
                ads.advDateExpire = DateTime.Parse(txtDateExpire.Text);
                ads.advLink = txtLink.Text;
                if (FileUpload1.HasFile)
                {
                    ads.advImageLink = FileUpload1.FileName;
                }

                if (c.updateAdvertisement(ads))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListAdsPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                itemId = int.Parse(Session["id"].ToString());

                if (c.deteleAdvertisement(itemId))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListAdsPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("cmd");
            Response.Redirect("~/WebAdmin/AdsListAdsPage.aspx");
        }

        private void loadContact()
        {
            c = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");

            cbbContact.DataSource = c.searchAllAdvertisementContacts();
            cbbContact.DataValueField = "advContId";
            cbbContact.DataTextField = "advContName";
            cbbContact.DataBind();

            c.Close();
        }

        private void loadBlock()
        {
            c = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");

            cbbBlock.DataSource = c.searchAllAdvertisementBlocks();
            cbbBlock.DataValueField = "blkId";
            cbbBlock.DataTextField = "blkName";
            cbbBlock.DataBind();

            c.Close();
        }

        private void fillData()
        {
            c = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");

            itemId = int.Parse(Session["id"].ToString());
            ads = c.searchAdvertisementById(itemId);
            
            cbbContact.SelectedValue = ads.advContId.ToString();
            cbbBlock.SelectedValue = ads.advBlockId;
            txtDateExpire.Text = ads.advDateExpire.ToString();
            txtLink.Text = ads.advLink;
            txtImageLink.Text = ads.advImageLink;

            c.Close();
        }
    }
}